$(function(e){
	dataInit();
});

function dataInit(){
	eventInit();
	$('#messageTable').bootstrapTable({
		url: path+'f2cc2f3acad94decb059d4fb9ca58f4b', method: 'post', striped: true, cache: false, pagination: true,
        contentType: "application/x-www-form-urlencoded", sidePagination: "server", pageSize: 10, uniqueId: "id", detailView: false,
        queryParams: function (params) {
        	return { limit: params.limit, offset: params.offset, searchParams : $("#searchParams").val()};
        },//传递参数（*）
        columns:[{field: 'Number', title: '序号', align: 'center', width: '50', formatter: function (value, row, index) {
        			return index+1;
        		}}, 
        		{field: 'optionText', title: '反馈信息', align: 'center', width: '500', formatter: function (value, row, index) {
        			if(isNull(value)){
	            		value = '-';
	            	}
        			return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'familyName', title: '反馈用户', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'createTime', title: '反馈时间', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'optionType', title: '反馈类型', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}
	            	return '<a style="word-wrap:break-word;">'+value+'</a>';
	            }},
	            {field: 'optionImg', title: '图片', align: 'center', width: '150', formatter: function (value, row, index) {
	            	if(isNull(value)){
	            		value = '-';
	            	}else{
	            		value = 'http://daxingzhiye.com/wechatPre/'+value;
	            	}
	            	return '<img style="width:40px;height:40px;" src="'+value+'"/>';;
	            }},
	            {field: 'operate', title: '操作', align: 'center', width: '210', events: {
	                'click .RoleOfB': function (e, value, row, index) {
	                	qiao.bs.confirm("确定要删除此项吗？",function(){
		                	deleteOption(row.id);
		                },function(){});
	                },
	            },
	            formatter: function (value, row, index) {
	            	return [
	        	        '<button type="button" class="RoleOfB btn btn-default  btn-sm" style="margin-right:15px;">删除</button>',
	    	        ].join('');
	            }
	            
        }],
        onLoadSuccess: function(data){  //加载成功时执行
        	if(data.returnCode!=0){
        		qiao.bs.msg({msg:data.returnMessage,type:'danger'});
        	}
        }
    });
	
	$("#addOptionForm").bootstrapValidator({
		fields : {
			addOptionText : {validators : {notEmpty : {message : '反馈信息不能为空.'},stringLength : {min : 2, max : 1000,message : '反馈信息长度必须在2到1000位之间'}}},
		},
		submitHandler : function(validator, form, submitButton){
			var addOptionText = $("#addOptionText").val();
			AjaxPostUtil.request({url : path+"55259b07a4ae4d1397839b9382fd9610", params : {optionText : addOptionText}, type : 'json', callback : function(json){
				if(json.returnCode == 0){
					qiao.bs.msg({msg : "意见反馈添加成功.", type : 'success'});
					$("#addOptionModal").modal("hide");
					$('#messageTable').bootstrapTable('refresh',{url : path+'f2cc2f3acad94decb059d4fb9ca58f4b'});
				}else{
					qiao.bs.msg({msg : json.returnMessage, type : 'danger'});
				}
			}});
		}
	});
}

function eventInit(){
	$('body').on('click', '#addOption', function(e){
		$("#addOptionText").val("");
		$("#addOptionModal").modal("show");
	});
	
	//回车模糊搜索
	$('body').keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
        	var params = {
        			limit : 10,
        			offset : 0,
        			searchParams : $("#searchParams").val()
        	}
        	AjaxPostUtil.request({url : path+"f2cc2f3acad94decb059d4fb9ca58f4b", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			$('#messageTable').bootstrapTable('refresh',{url : path+'f2cc2f3acad94decb059d4fb9ca58f4b'});
        		}else{
        			qiao.bs.msg({msg : '参数错误.', type : 'danger'});
        		}
        	}});
        }
	});
	
	//刷新表单
	$('body').on('click', '#refreshDiv', function(e){
		$('#messageTable').bootstrapTable('refresh',{url : path+'f2cc2f3acad94decb059d4fb9ca58f4b'});
		qiao.bs.msg({msg : "刷新成功.", type : 'success'});
	});
}

function deleteOption(rowId){
	AjaxPostUtil.request({url : path+"9641159ad37246239784971ae58e71da", params : {rowId : rowId}, type : 'json', callback : function(jsonMation){
		if(jsonMation.returnCode == 0){
			qiao.bs.msg({msg : "删除成功.", type : 'success'});
			$('#messageTable').bootstrapTable('refresh',{url : path+'f2cc2f3acad94decb059d4fb9ca58f4b'});
		}else{
			qiao.bs.msg({msg : "参数错误.", type : 'danger'});
		}
	}});
}